namespace NHCodeFirst.FluentMapper
{
    using System;
    using Enums;
    using HbmModel;

    public class PropertyMapper : TypeAndColumnMapper<PropertyMapper>
    {
        private readonly Property property;
        internal PropertyMapper(Property property)
            : base(property.Type, property.Column)
        {
            this.property = property;
            child = this;
        }

        public PropertyMapper Formula(string value)
        {
            property.Formula = value;
            return this;
        }

        public PropertyMapper Generated(bool value)
        {
            property.Generated = value.ToString().ToLower();
            return this;
        }

        public PropertyMapper Insert(bool value)
        {
            property.Insert = value.ToString().ToLower();
            return this;
        }

        public PropertyMapper Lazy(bool value)
        {
            property.Lazy = value.ToString().ToLower();
            return this;
        }

        public PropertyMapper OptimisticLock(bool value)
        {
            property.OptimisticLock = value.ToString().ToLower();
            return this;
        }

        public PropertyMapper Update(bool value)
        {
            property.Update = value.ToString().ToLower();
            return this;
        }

        public PropertyMapper Access(Access access, Naming? namingStrategy = null)
        {
            return Access(access.ToString().ToLower(), namingStrategy);
        }

        public PropertyMapper Access<T>(Naming? namingStrategy = null)
        {
            return Access(typeof(T).FullName, namingStrategy);
        }

        public PropertyMapper Access(Type type, Naming? namingStrategy = null)
        {
            return Access(type.FullName, namingStrategy);
        }

        private PropertyMapper Access(string access, Naming? naming)
        {
            property.Access = access;
            if (naming.HasValue)
                property.Access += "." + EnumsToHbmString.GetAccessNamingStrategy(naming.Value);
            return this;
        }
    }
}